package object.cars {
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.library.assets.AssetType;
	import away3d.materials.ColorMaterial;
	import away3d.materials.TextureMaterial;
	import away3d.primitives.PlaneGeometry;
	import away3d.textures.BitmapTexture;
	import flash.events.Event;
	import loader.LoaderManager;
	import object.parts.Wheel;
	
	/**
	 * 汽车基类
	 * @author yungzhu@gmail.com
	 * Morn UI：flash轻量级，高性能，可视化UI解决方案 https://code.google.com/p/morn/
	 */
	public class CarBase extends ObjectContainer3D {
		private var _body:Mesh; //车身
		private var _bumper:Mesh; //车壳
		private var _glass:Mesh; //玻璃
		private var _interior:Mesh; //内部
		private var _shadow:Mesh = new Mesh(new PlaneGeometry(1120, 1120)); //阴影
		
		private var _bodyMater:ColorMaterial;
		private var _bumperMater:ColorMaterial;
		private var _glassMater:ColorMaterial;
		private var _interiorMater:TextureMaterial;
		private var _shadowMater:TextureMaterial;
		
		//轮胎
		protected var _frontRightWheel:Wheel;
		protected var _frontLeftWheel:Wheel;
		protected var _backRightWheel:Wheel;
		protected var _backLeftWheel:Wheel;
		
		public function CarBase(body:ColorMaterial, bumper:ColorMaterial, glass:ColorMaterial, wheel:ColorMaterial) {
			_bodyMater = body;
			_bumperMater = bumper;
			_glassMater = glass;
			_interiorMater = new TextureMaterial();
			_shadowMater = new TextureMaterial();
			
			//轮胎			
			_frontRightWheel = new Wheel(wheel, "right");
			_frontLeftWheel = new Wheel(wheel, "left");
			_backRightWheel = new Wheel(wheel, "right");
			_backLeftWheel = new Wheel(wheel, "left");
			
			y = -50;
			rotationY = -40;
		}
		
		protected function loadModel(path:String):void {
			LoaderManager.instance.load(path + "InteriorSkin.jpg", AssetType.TEXTURE, setTexture, [_interiorMater]);
			LoaderManager.instance.load(path + "CarShadow.png", AssetType.TEXTURE, setTexture, [_shadowMater]);
			LoaderManager.instance.load(path + "body.3DS", AssetType.MESH, setMesh, ["_body"]);
			LoaderManager.instance.load(path + "bumper.3DS", AssetType.MESH, setMesh, ["_bumper"]);
			LoaderManager.instance.load(path + "glass.3DS", AssetType.MESH, setMesh, ["_glass"]);
			LoaderManager.instance.load(path + "interior.3DS", AssetType.MESH, setMesh, ["_interior"]);
			LoaderManager.instance.addEventListener(Event.COMPLETE, onAllLoadComplete);
		}
		
		private function onAllLoadComplete(e:Event):void {
			LoaderManager.instance.removeEventListener(Event.COMPLETE, onAllLoadComplete);
			initParts();
		}
		
		private function setTexture(mater:TextureMaterial, asset:*):void {
			mater.texture = new BitmapTexture(asset.bitmapData);
		}
		
		private function setMesh(mesh:String, asset:*):void {
			this[mesh] = asset as Mesh;
		}
		
		private function initParts():void {
			_body.material = _bodyMater;
			_bumper.material = _bumperMater;
			_glass.material = _glassMater;
			_interior.material = _interiorMater;
			_shadow.material = _shadowMater;
			_shadowMater.alpha = 0.5;
			_shadow.y = 3;
			addChildren(_body, _bumper, _glass, _interior, _shadow, _frontRightWheel, _frontLeftWheel, _backRightWheel, _backLeftWheel);
		}
	}
}